Micron Document
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| SparkN0de-git | SparkN0de |
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


Displaying Rendered • View rawDownload


market.mu main (13cc24bf) Text, 5.94 KB

import core
import os
import json
import uuid
import time


categories = [
'Food',
'Tools',
'Equipment'
]
listing_expires = (60 * 60 * 24 * 14)


def read_category(category):
try:
with open(core.data_directory + '/market/' + category + '.json', 'r') as market_file:
markets = json.load(market_file)
return markets
except:
market = []
market_object = json.dumps(market, indent=4)
with open(core.data_directory + '/market/' + category + '.json', "w") as market_file:
market_file.write(market_object)
return market

def add_listing(category, username, title, description, asking):
title = title.replace(', '\)
description = description.replace(', '\)
asking = asking.replace(', '\)
markets = read_category(category)
markets.insert(0, {'username': username, 'id': str(uuid.uuid4()), 'title':title, 'description': description, 'asking': asking, "time": time.time()})
markets_object = json.dumps(markets, indent=4)
with open(core.data_directory + '/market/' + category + '.json', "w") as markets_file:
markets_file.write(markets_object)

def delete_listing(category, listing_id):
markets = read_category(category)
n_list = 0
for item in markets:
if item['id'] == listing_id:
del_n = n_list
n_list = n_list + 1
del markets[del_n]
markets_object = json.dumps(markets, indent=4)
with open(core.data_directory + '/market/' + category + '.json', "w") as markets_file:
markets_file.write(markets_object)


if 'link_id' not in os.environ:
os.environ['link_id'] = 'local_test'

current_session = core.get_current_session(os.environ['link_id'])

core.header(current_session)

if current_session:
if 'var_new_listing' in os.environ:
print('Title: ')
print()
print('Asking: ')
print()
print('Description: ')
print()
print('<Post> + ']')

if 'var_del_listing_id' in os.environ:
delete_listing(os.environ['var_category'], os.environ['var_del_listing_id'])
print('Deleted listing.')
print('<Back to ' + os.environ['var_category' + '>' + core.page_path + '/market.muategory=' + os.environ['var_category'] + ']')

if 'field_title' in os.environ and 'field_asking' in os.environ and 'field_description' in os.environ:
print('Listing added.')
print('<Back to ' + os.environ['var_category' + '>' + core.page_path + '/market.muategory=' + os.environ['var_category'] + ']')
add_listing(os.environ['var_category'], current_session['username'], os.environ['field_title'], os.environ['field_description'], os.environ['field_asking'])

if 'var_category' not in os.environ and 'var_listing_id' not in os.environ and 'var_new_listing' not in os.environ and 'var_del_listing_id' not in os.environ:
print('Market')
print('')
print()
for category in categories:
print()

if 'var_listing_id' in os.environ:
items = read_category(os.environ['var_category'])
for item in items:
if item['id'] == os.environ['var_listing_id']:
delete = ''
try:
if current_session['username'] == item['username']:
delete = '<Delete> + '|del_listing_id=' + item['id'] + ']'
except:
pass
print('<Back to ' + os.environ['var_category' + '>' + core.page_path + '/market.muategory=' + os.environ['var_category'] + '] <' + item['username' + ' Profile>' + core.page_path + '/profile.musername=' + item['username'] + '] <Message ' + item['username' + '>' + core.page_path + '/messages.muew_message=' + item['username'] + '|subject=Market: ' + item['title'] + '] ' + delete)
print()
print('' + item['title'] + '')
print()
print(core.convert_time(item['time']))
print(item['asking'])
print('' + item['description'])
print()

if 'var_category' in os.environ and 'var_listing_id' not in os.environ and 'var_new_listing' not in os.environ and 'var_del_listing_id' not in os.environ and 'field_title' not in os.environ:
items = read_category(os.environ['var_category'])
print('Market: ' + os.environ['var_category'] + '')
print()
print('')
print()
if current_session:
print('<New Listing in ' + os.environ['var_category' + '>' + core.page_path + '/market.muew_listing=' + os.environ['var_category'] + ']')
print()
for item in items:
if time.time() < item['time'] + listing_expires:
print('<View> + '|category=' + os.environ['var_category'] + '] ' + core.convert_time(item['time']) + ' | ' + item['title'] + ' | ' + item['asking'])
print()

core.footer()


──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────